package com.ly.emp.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.common.core.bean.R;
import com.ly.common.core.bean.dto.CommonBatchUpdateDTO;
import com.ly.emp.bean.dto.HubTeamIsEnableDTO;
import com.ly.emp.entity.HubTeamCaseOrg;
import com.ly.emp.mapper.HubTeamCaseOrgMapper;
import com.ly.emp.service.IHubTeamCaseOrgService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 模块配置-org-impl
 *
 * @author luoyun
 * @Date: 2025-05-12
 */
@Service
public class HubTeamCaseOrgServiceImpl extends ServiceImpl<HubTeamCaseOrgMapper, HubTeamCaseOrg> implements IHubTeamCaseOrgService {

    @Override
    public R getId(String id) {
        HubTeamCaseOrg entity = this.getById(id);
        if (entity == null) {
            return R.fail("未找到对应数据");
        }
        return R.ok(entity);
    }

    @Override
    public R getList(HubTeamCaseOrg entity) {
        List<HubTeamCaseOrg> list = this.list(
                new LambdaQueryWrapper<HubTeamCaseOrg>()
                        .eq(StrUtil.isNotEmpty(entity.getOrgCode()), HubTeamCaseOrg::getOrgCode, entity.getOrgCode())
                        .eq(StrUtil.isNotBlank(entity.getCaseStatus()), HubTeamCaseOrg::getCaseStatus, entity.getCaseStatus())
                        .orderByAsc(HubTeamCaseOrg::getDisplayOrder)
                        .orderByAsc(HubTeamCaseOrg::getSaveCode)
        );
        return R.ok(list);
    }

    @Override
    public R getPage(HubTeamCaseOrg entity, Page<HubTeamCaseOrg> page) {
        IPage<HubTeamCaseOrg> pageList = this.page(
                page,
                new LambdaQueryWrapper<HubTeamCaseOrg>()
                        .like(StrUtil.isNotEmpty(entity.getCaseName()), HubTeamCaseOrg::getCaseName, entity.getCaseName())
                        .eq(HubTeamCaseOrg::getOrgCode, entity.getOrgCode())
                        .orderByAsc(HubTeamCaseOrg::getDisplayOrder)
        );
        return R.ok(pageList);
    }

    @Override
    public R add(HubTeamCaseOrg entity) {
        boolean flag = this.save(entity);
        return flag ? R.ok(true, "创建成功") : R.fail("创建失败");
    }

    @Override
    public R edit(HubTeamCaseOrg entity) {
        boolean flag = this.updateById(entity);
        return flag ? R.ok(true, "修改成功") : R.fail("修改失败");
    }

    @Override
    public R delete(String id) {
        boolean flag = this.removeById(id);
        return flag ? R.ok(true, "删除成功") : R.fail("删除失败");
    }

    @Override
    public R deleteBatch(String ids) {
        boolean flag = this.removeByIds(Arrays.asList(ids.split(",")));
        return flag ? R.ok(true, "批量删除成功") : R.fail("批量删除失败");
    }

    @Override
    public R updateBatch(CommonBatchUpdateDTO<HubTeamCaseOrg> dto) {
        List<HubTeamCaseOrg> datas = dto.getDatas();
        boolean flag = this.saveOrUpdateBatch(datas);
        return flag ? R.ok(true, "批量修改成功") : R.fail("批量修改失败");
    }

    @Override
    public R isEnable(HubTeamIsEnableDTO dto) {
        boolean update = this.update(
                new LambdaUpdateWrapper<HubTeamCaseOrg>()
                        .eq(HubTeamCaseOrg::getId, dto.getId())
                        .set(HubTeamCaseOrg::getCaseStatus, dto.getIsEnable())
        );
        return update ? R.ok(true, "操作成功") : R.fail("操作失败");
    }

    @Override
    public R isCount(HubTeamIsEnableDTO dto) {
        boolean update = this.update(
                new LambdaUpdateWrapper<HubTeamCaseOrg>()
                        .eq(HubTeamCaseOrg::getId, dto.getId())
                        .set(HubTeamCaseOrg::getCaseCount, dto.getIsEnable())
        );
        return update ? R.ok(true, "操作成功") : R.fail("操作失败");
    }
}
